#include "stdafx.h"
#include <d3dx9effect.h>
#include "car_seter.h"
#include "ltbasedefs.h"
#include "lteffectimpl.h"
#include "../ltclientshell.h"
#include <iltclient.h>
#include "../shader_mgr.h"
#include "shader_light_system.h"
#include "../d3d_texture_manager.h"
extern IDirect3DCubeTexture9* g_apCubeMap ;

car_seter::car_seter(shader_manager* mgr, const std::string& cubename) :
    arg_seter(mgr),
    m_cubemap_name(cubename),
    m_factor(1.0f),
    m_cube(0),
    m_tex_mgr(mgr->texture_manager())
{
	m_light_pos[0] = -16.0f ;
	m_light_pos[1] = 52.0f;
	m_light_pos[2] = -256.0f ;

	m_tail_color[0]=m_tail_color[1]=m_tail_color[2]=0.0f;
	m_tail_color[3]=1.0f;
}

car_seter::~car_seter()
{

}

void car_seter::set_light_pos(const LTVector& _pos, float factor, float radius) {    
	m_light_pos[0]=_pos.x;
	m_light_pos[1]=_pos.y;
	m_light_pos[2]=_pos.z;
	m_light_pos[3]=1;
	m_factor = factor ;
	m_radius = radius ;
  
}

void car_seter::set_args( LTEffectShader *pEffect,const LTShaderDeviceState &ShaderDeviceState )
{
	ShaderDeviceState.GetMatrix(LTShaderDeviceState::LTMatrixType::MATRIXTYPE_VIEW,false, m_view) ;
	LTShaderDeviceState::LTLightDesc dect ;
	//for(int i = 0 ;i < 3 ; i++) {
  LTVector pos ;
  g_pLTClient->GetObjectPos(get_obj(), &pos) ;
  
  shader_manager* mgr = get_shader_manager() ;
  mgr->light_system()->range_light_by_distance(pos) ;
  mgr->light_system()->use_lights(pEffect, m_factor, m_light_pos, m_radius, 4);
  /*
		if(dect.m_Active) {
			float position[4] ;
			position[0] = dect.m_Position.x ;
			position[1] = dect.m_Position.y ;
			position[2] = dect.m_Position.z ;
			position[3] = 0.0 ;
			pEffect->SetFloatArray("g_light[0].position", position, 3) ;
			position[0] = dect.m_Diffuse.r ;
			position[1] = dect.m_Diffuse.g ;
			position[2] = dect.m_Diffuse.b ;
			position[3] = dect.m_Diffuse.a ;
			pEffect->SetFloatArray("g_light[0].color", position, 4) ;

		}
  */
    //}

	
	ShaderDeviceState.GetMatrix(LTShaderDeviceState::LTMatrixType::MATRIXTYPE_WORLD0, false, m_world);
	ShaderDeviceState.GetMatrix(LTShaderDeviceState::LTMatrixType::MATRIXTYPE_VIEWPROJECTION, false, m_view_proj);
	pEffect->SetMatrix("World",*m_world);
	pEffect->SetMatrix("ViewProj",*m_view_proj);
	m_world->Inverse() ;
	pEffect->SetMatrix("WorldInv", *m_world) ;
	m_view->Inverse() ;
	pEffect->SetMatrix("ViewInv", *m_view) ;

	pEffect->SetVector("tail_light_color",m_tail_color);
	
	//pEffect->SetVector("lightpos",m_light_pos);
  ID3DXEffect* pD3dEffect = ((LTEffectImpl*)(pEffect))->GetEffect() ;
  D3DXHANDLE handleCubeMap ;
  handleCubeMap = pD3dEffect->GetParameterByName(NULL, "cubemap") ;
  IDirect3DCubeTexture9* ttx  = 0 ;
  
      
  ttx  = m_tex_mgr->get_texture<IDirect3DCubeTexture9*>(m_cubemap_name) ;
  if(g_apCubeMap) pD3dEffect->SetTexture(handleCubeMap, g_apCubeMap) ;
      //pD3dEffect->SetTexture(handleCubeMap, ttx) ;
  


}

void car_seter::set_tail_color( float _r,float _g,float _b )
{
	m_tail_color[0]=_r;
	m_tail_color[1]=_g;
	m_tail_color[2]=_b;
}
